package com.appbee.songs.client.ui;

import java.util.List;

import javax.inject.Inject;

import com.appbee.core.client.design.PageStyle;
import com.appbee.core.client.listener.ValueChangeListener;
import com.appbee.gdata.youtube.client.model.SimpleEntry;
import com.appbee.songs.client.design.SongListResources;
import com.appbee.songs.client.presenter.ISongSelectionPresenter;
import com.appbee.songs.client.presenter.ISongsDataPresenter;
import com.appbee.songs.client.text.SongsStrings;
import com.appbee.songs.shared.SongProxy;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class SongsListView extends Composite implements ISongsListView,
		ValueChangeListener
{

	private static SongsListViewUiBinder uiBinder = GWT
			.create(SongsListViewUiBinder.class);

	interface SongsListViewUiBinder extends UiBinder<Widget, SongsListView>
	{
	}

	class SongCell extends AbstractCell<SongProxy>
	{

		@Override
		public void render(Context context, SongProxy value, SafeHtmlBuilder sb)
		{
			sb.appendEscaped(value.getName() + " - " + value.getArtist());
		}

	}

	@UiField(provided = true)
	PageStyle style;

	@UiField(provided = true)
	ISongsStyle songsStyle;

	@UiField(provided = true)
	SongsStrings songsStrings;

	@UiField(provided = true)
	SongSearchView searchBox;

	@UiField(provided = true)
	CellList<SongProxy> cellList;

	@UiField(provided = true)
	SimplePager pager;

	private ISongsDataPresenter presenter;

	@Inject
	public SongsListView(	PageStyle style,
							ISongsStyle songsStyle,
							SongListResources listResources,
							SongsStrings songsStrings,
							SongSearchView searchBox)
	{
		this.style = style;
		this.songsStyle = songsStyle;
		this.songsStrings = songsStrings;
		this.searchBox = searchBox;
		cellList = new CellList<SongProxy>(new SongCell(), listResources);
		SingleSelectionModel<SongProxy> selectionModel = new SingleSelectionModel<SongProxy>();
		cellList.setSelectionModel(selectionModel);
		pager = new SimplePager(TextLocation.CENTER);
		pager.setDisplay(cellList);

		initWidget(uiBinder.createAndBindUi(this));

		searchBox.setValueChangeListener(this);

		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler()
				{

					@Override
					public void onSelectionChange(SelectionChangeEvent event)
					{

					}
				});
	}

	@Override
	public ISongFilterView getFilterView()
	{
		return searchBox;
	}

	@Override
	public void setPresenter(ISongsDataPresenter presenter)
	{
		this.presenter = presenter;
	}

	@Override
	public HasData<SongProxy> getDataDisplay()
	{
		return cellList;
	}

	@Override
	public void clearSongSelection()
	{
	}

	@Override
	public void setPlayerPanelShowing(boolean playerPanelShowing)
	{
	}

	@Override
	public void setTubeListData(List<SimpleEntry> list)
	{
	}

	@Override
	public void setSelectedTube(SimpleEntry simpleEntry)
	{
	}

	@Override
	public void clearPlayer()
	{
	}

	@Override
	public void setLyrics(String string)
	{
	}

	@Override
	public void setSongsSelectionPresenter(ISongSelectionPresenter presenter)
	{
	}

	@Override
	public void onValueChange(String value)
	{
		presenter.onSearch();
	}

}
